//
//  CNM_EXButton.swift
//  swiftTXH
//
//  Created by Mac mini on 2020/12/11.
//  Copyright © 2020 Mac mini. All rights reserved.
//

import UIKit

extension UIButton {
    // 下划线文本按钮
    public func cnm_setUnderLine(title: String, andTitleColor color: UIColor, andFont font: UIFont, andUnderlineColor lineColor: UIColor) {
        let attri = NSAttributedString(string: title, attributes: [
            NSAttributedString.Key.foregroundColor : color,
            NSAttributedString.Key.font: font,
            NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
            NSAttributedString.Key.underlineColor: lineColor
        ])
        self.setAttributedTitle(attri, for: .normal)
    }
}

extension UIButton {
    public enum CNMButtonImageLayoutStyle {
        case top, left, right, bottom
    }
    
    public func cnm_layout(with style: CNMButtonImageLayoutStyle, space: Float) {
        let imageWidth = self.imageView?.cnm_width ?? 0.0
        let imageHeight = self.imageView?.cnm_height ?? 0.0
        
        let labelWidth = self.titleLabel?.intrinsicContentSize.width ?? 0.0
        let labelHeight = self.titleLabel?.intrinsicContentSize.height ?? 0.0
        
        var imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        var labelEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        switch style {
        case .top:
            imageEdgeInsets = UIEdgeInsets(top: CGFloat(-labelHeight) - CGFloat(space)/2.0,
                                           left: 0,
                                           bottom: 0,
                                           right: CGFloat(-labelWidth))
            labelEdgeInsets = UIEdgeInsets(top: 0,
                                           left: CGFloat(-imageWidth),
                                           bottom: CGFloat(-imageHeight) - CGFloat(space)/2.0,
                                           right: 0.0)
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0.0,
                                           left: CGFloat(-space)/2.0,
                                           bottom: 0,
                                           right: CGFloat(space/2.0))
            labelEdgeInsets = UIEdgeInsets(top: 0.0,
                                           left: CGFloat(space)/2.0,
                                           bottom: 0,
                                           right: CGFloat(-space/2.0))
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0.0,
                                           left: CGFloat(labelWidth) + CGFloat(space)/2.0,
                                           bottom: 0,
                                           right: CGFloat(-labelWidth) - CGFloat(space/2.0))
            labelEdgeInsets = UIEdgeInsets(top: 0.0,
                                           left: CGFloat(-imageWidth) - CGFloat(space/2.0),
                                           bottom: 0,
                                           right: CGFloat(imageWidth) + CGFloat(space/2.0))
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0,
                                           bottom: CGFloat(-labelHeight) - CGFloat(space/2.0),
                                           right: CGFloat(-labelWidth))
            labelEdgeInsets = UIEdgeInsets(top: CGFloat(-imageHeight) - CGFloat(space/2.0),
                                           left: CGFloat(-imageWidth),
                                           bottom: 0.0, right: 0.0)
        }
        
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
    }
}
